//弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType, callback) {
	var interval;
	var timeout = 10000;
	// 		interval = setInterval(fun, 1000);
	var h = $('#multiQueryMessage').parent().html();
	if (h == msgString) {
		return false
	}
	var m = null;
	if (callback) {
		m = $.messager.alert(title, msgString, msgType, callback);
	} else {
		m = $.messager.alert(title, msgString, msgType);
	}
	function fun() {
		/* timeout=timeout-1000;
		if (timeout==0) {
			$('.messager-body').window('close');
			clearInterval(interval);
		}
		$(".panel-title").each(function(){
			if($(this).html().indexOf('系统提示')>=0){
				$(this).text("");
				$(this).append(title+"（"+(timeout/1000)+"秒后自动关闭）");
			}
		}); */
	}
}
//加载遮罩
function openMask(id) {
	var wrap = $("#" + id);
	$("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: wrap.width(), height: wrap.height() }).appendTo(wrap);
	$("<div class=\"datagrid-mask-msg\"></div>").html('正在处理，请稍待....').appendTo(wrap).css({ display: "block", left: (wrap.width() - $("div.datagrid-mask-msg", wrap).outerWidth()) / 2, top: (wrap.height() - $("div.datagrid-mask-msg", wrap).outerHeight()) / 2 });
}
//关闭遮罩
function closeMask(id) {
	var wrap = $("#" + id);
	wrap.find("div.datagrid-mask-msg").remove();
	wrap.find("div.datagrid-mask").remove();
}
function noSelectData() {
	msgShow("");
}
function sysMsgShow(msgString) {
	msgShow('系统提示', msgString, 'info');
}
function sysErrorShow(msgString, fn) {
	msgShow('系统提示', msgString, 'error', fn);
}

var temp = object(tempAbs);
temp.sampleInfos = [];
temp.address = false;
temp.read = function (obj, bn) {
	bn.setSampleInfos();
	if (obj && obj.customerCode) {
		bn.setComboBox('customerCode', obj.customerCode);
	}
	$(".second-table .field input").each(function () {
		var id = $(this).attr('id');
		if (id && id.indexOf('sampleType_') < 0) {
			if ($(this).hasClass('easyui-datebox')) {
				bn.setDat(id);
			} else {
				var type = $(this).attr('type');
				if (type == 'text' || type == 'date' || type == 'number') {
					bn.setVl(id);
				}
			}
		}
	});
	$(".second-table .field select").each(function () {
		var id = $(this).attr('id');
		bn.setSel(id);
	});
	if (obj.sampleType) {
		var sampleTypeArr = obj.sampleType.split(',');
		$("input:checkbox[name^='sampleType_']").each(function (i) {
			var id = $(this).attr('name');
			setCkArr(id, sampleTypeArr[i]);
		});
	}

	var all = {};
	$(".second-table .group-field").children('input').each(function () {
		var id = $(this).attr('id');
		all[id.split('_')[0]] = 1;
	});
	for (var key in all) {
		bn.setCkGrp(key, $("input:checkbox[id^='" + key + "_']").length);
	}
	//处理多选框start
	$(".group-field input").each(function () {
		if ($(this).is(":checked")) {
			var id = $(this).attr('id');
			var arr = id.split('_');
			bn.obj[arr[0]] = arr[1];
		}
	});
	var multiObj = {};
	$(".multi-field input").each(function () {
		var id = $(this).attr('id');
		var tarr = id.split('_');
		if (tarr.length == 2) {
			var vArr = multiObj[tarr[0]];
			if (!vArr) {
				vArr = [];
			}
			vArr.push(id);
			multiObj[tarr[0]] = vArr;
		}
	});
	for (var key in multiObj) {
		if (obj[key]) {
			var values = obj[key].split(',');
			for (var i = 0; i < values.length; i++) {
				$("#" + key + "_" + values[i]).attr('checked', true);
			}
		}
	}
	//处理多选框end
	if (typeof (readExtends) != 'undefined') {
		readExtends(obj, bn);
	}
}

temp.save = function (bn) {
	bn.getSampleInfos(temp.sampleInfos);
	//	bn.getComboGrid('customerCode');
	bn.vl('sampleNum');
	$(".second-table .field input").each(function () {
		var id = $(this).attr('id');
		if (id && id.indexOf('sampleType_') < 0) {
			var type = $(this).attr('type');
			if ($(this).hasClass('easyui-datebox')) {
				bn.dat(id);
			} else {
				if (type == 'text') {
					bn.vl(id);
				} else if (type == 'date') {
					bn.vl(id);
				} else if (type == 'number') {
					bn.vl(id);
				}
			}
		}
	});
	$(".second-table .field select").each(function () {
		var id = $(this).attr('id');
		bn.sel(id);
	});
	var sampleTypeArr = [];
	var countSampleType = {};
	$("input:checkbox[name^='sampleType_']").each(function () {
		countSampleType[$(this).attr('name')] = 1;
	});
	for (var key in countSampleType) {
		var val = ckArr(key);
		if (val) {
			sampleTypeArr.push(val);
		}
	}
	bn.obj.sampleType = sampleTypeArr.join(',');
	//处理单选框字段
	$(".group-field input").each(function () {
		if ($(this).is(":checked")) {
			var id = $(this).attr('id');
			var arr = id.split('_');
			bn.obj[arr[0]] = arr[1];
		}
	});
	//处理多选框start
	var multiObj = {};
	$(".multi-field input").each(function () {
		var id = $(this).attr('id');
		var tarr = id.split('_');
		if (tarr.length == 2) {
			var vArr = multiObj[tarr[0]];
			if (!vArr) {
				vArr = [];
			}
			vArr.push(id);
			multiObj[tarr[0]] = vArr;
		}
	});
	for (var id in multiObj) {
		var arr = multiObj[id];
		var value = [];
		for (var i = 0; i < arr.length; i++) {
			if ($("#" + arr[i]).is(":checked")) {
				value.push(arr[i].split('_')[1]);
			}
		}
		bn.obj[id] = value.join(',');
	}
	//处理多选框end
	if (typeof (saveExtends) != 'undefined') {
		saveExtends(bn);
	}
}
temp.validateAbs = function (m) {
	var msg = '';
	if (m) msg += m;
	return msg;
}
//组合复选框变单选框
$(function () {
	var p1 = null;
	var sortIndex = 1;
	$(".sample-type").each(function (index) {
		var trArr = $(this).parent().parent().parent().parent().find('tr');
		var tTrArr = [];
		for (var i = 0; i < trArr.length; i++) {
			tTrArr.push(trArr[i]);
		}
		if (index == 0) {
			p1 = $(this).parent()[0];
			$(this).children("input:eq(0)").attr('name', "sampleType_" + sortIndex);
		} else {
			if (p1 != $(this).parent()[0] && Math.abs(tTrArr.indexOf(p1.parentNode) - tTrArr.indexOf($(this).parent().parent()[0])) > 1) {
				sortIndex++;
			}
			p1 = $(this).parent()[0];
			$(this).children("input:eq(0)").attr('name', "sampleType_" + sortIndex);
		}
	});
	var countObj = {};//统计有多少组需要单选的checkbox
	$(".second-table .group-field input").each(function () {
		var name = $(this).attr("name");
		countObj[name] = 1;
	});
	$(".second-table .sample-type input").each(function () {
		var name = $(this).attr("name");
		countObj[name] = 1;
	});
	for (var key in countObj) {
		$("input[type=checkbox][name='" + key + "']").click(function () {
			if ($(this).is(':checked')) {
				if ($(this).siblings('input').length > 0) {
					$(this).siblings('input').attr('checked', false);
				} else {
					$(this).parent('span').siblings().children('input').attr('checked', false);
				}
			}
		});
	}
	$("input:checkbox[id^='product_']").each(function () {
		var id = $(this).attr('id');
		temp.sampleInfos.push({ productCode: id.split('_')[1], projectCode: '' });
	});
});

function getJsonFromUrl() {
	var query = location.search.substr(1);
	var result = {};
	query.split("&").forEach(function (part) {
		var item = part.split("=");
		result[item[0]] = decodeURIComponent(item[1]);
	});
	return result;
}

var isEdit = false;
var sampleBaseId;
$(document).ready(function () {
	var obj = null;
	var isNew = true;
	if (!!getJsonFromUrl().edit) {
		var objJson = sessionStorage.getItem("sampleInfos");
		if (objJson) {
			try {
				obj = JSON.parse(objJson);
				sampleBaseId = obj.sampleBaseId;
				isNew = false;
				isEdit = true;
			} catch (err) {
			}
		}
	}
	console.dir(obj);
	temp.readAbs(obj, isNew);
});